﻿using System;

using NUnit.Framework;

using OpenQA.Selenium;

namespace mvcForum.Tests.WebUI {

	[TestFixture]
	public class N800_PostsTest {
		private static IWebDriver driver;

		#region Initialization and cleanup
		[TestFixtureSetUp]
		public static void TestInitialize() {
			driver = TestHelper.GetDriver();
			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.DisableAkismet();
			driver.DisableMultiPost();
			driver.DisableNewUserTrial();
			driver.LogOff();
		}

		[TestFixtureTearDown]
		public static void TestCleanup() {
			if (driver != null) {
				driver.Close();
			}
		}
		#endregion

		[Test]
		public void PostTopicReply() {
			String forum = "Forum 3";
			String topicTitle = "The topic, reply to this!";

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateTopic(forum, topicTitle, "Nothing to write, just get the test started!");
			driver.LogOff();

			driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password);

			driver.GoToForum(forum);

			IWebElement topicLink = driver.FindElement(By.LinkText(topicTitle));
			if (topicLink != null) {
				topicLink.Click();
				driver.Wait(1);

				// This is the url from the forum link, let's remove the "sincelast" bit!
				String topicURL = driver.Url.ToLower().Replace("/sincelast#sincelast", "");

				driver.FindElement(By.LinkText("Post a reply")).Click();

				String postTitle = "A new subject, just to test it!";
				String postBody = "The first reply done in the test.";
				IWebElement subjectBox = driver.FindElement(By.Name("Subject"));
				Assert.IsNotNull(subjectBox);
				subjectBox.SendKeys(postTitle);

				IWebElement bodyBox = driver.FindElement(By.Name("Body"));
				Assert.IsNotNull(bodyBox);
				bodyBox.SendKeys(postBody);

				IWebElement button = driver.FindElement(By.Name("post"));
				Assert.IsNotNull(button);
				button.Click();
				driver.Wait(2);

				Assert.AreEqual(topicURL + "?page=1", driver.Url.ToLower());

				Assert.AreEqual(true, driver.PageSource.Contains(postTitle));
				Assert.AreEqual(true, driver.PageSource.Contains(postBody));
			}
			else {
				Assert.Fail("Topic not found");
			}

			driver.LogOff();
		}

		[Test]
		public void PostPostReply() {
			String forum = "Forum 3";
			String topicTitle = "Parent topic!";

			driver.LogOn(Constants.Users.Admin.Email, Constants.Users.Admin.Password);
			driver.CreateTopic(forum, topicTitle, "Nothing to write, just get the test started!");
			driver.LogOff();

			driver.LogOn(Constants.Users.Regular.Email, Constants.Users.Regular.Password);

			driver.GoToForum(forum);

			IWebElement topicLink = driver.FindElement(By.LinkText(topicTitle));
			if (topicLink != null) {
				topicLink.Click(driver, "profilediv0");

				// This is the url from the forum link, let's remove the "sincelast" bit!
				String topicURL = driver.Url.ToLower().Replace("/sincelast#sincelast", "");

				driver.FindElement(By.XPath("//a[@title='Quote']")).Click();

				String postBody = "The first reply to a post done in the test.";

				IWebElement bodyBox = driver.FindElement(By.Name("Body"));
				Assert.IsNotNull(bodyBox);
				bodyBox.SendKeys(postBody);

				IWebElement button = driver.FindElement(By.Name("post"));
				Assert.IsNotNull(button);
				button.Click();
				driver.Wait(1);

				Assert.AreEqual(topicURL + "?page=1", driver.Url.ToLower());

				Assert.AreEqual(true, driver.PageSource.Contains(postBody));
			}
			else {
				Assert.Fail("Topic not found");
			}

			driver.LogOff();
		}
	}
}